﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using E4RPDevExpress10;
using HouseRental.DataSets;

namespace HouseRental.Controls
{
    public partial class EstimateList : UserControl, IGridView
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.EstimateSaved += delegate { SetGridDataSource(); };
                _controller.HouseRentalChange += delegate { SetGridDataSource(); };                
            }
        }

        #endregion

        public EstimateList()
        {
            InitializeComponent();
            FormatGridColumns();
            gridView.DoubleClick += GridViewDoubleClick;
            gridView.OptionsView.ShowFooter = true;
        }

        #region Private Methods

        private void SetupHouseRentalData()
        {
            condominioDataSet.Clear();
            condominioDataSet.Merge(_controller.HouseRentals());
            houseRentalRepositoryItemLookUpEdit.DataSource = condominioDataSet.Condomínio;
            houseRentalRepositoryItemLookUpEdit.DisplayMember = condominioDataSet.Condomínio.NomeColumn.ToString();
            houseRentalRepositoryItemLookUpEdit.ValueMember = condominioDataSet.Condomínio.IdColumn.ToString();
            houseRentalRepositoryItemLookUpEdit.Columns.Clear();
            houseRentalRepositoryItemLookUpEdit.Columns.AddRange(new[] {
                new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Nome", "Nome", 70, FormatType.None, "", true, HorzAlignment.Near)});
        }

        private void SetGridDataSource()
        {
            SetupHouseRentalData();
            OrcamentoDataSet dataSet = _controller.Estimates();
            gridControl.DataSource = null;
            gridControl.DataSource = dataSet;
            gridControl.DataMember = dataSet.Orcamento.TableName;
            FormatGridColumns();
        }

        private void FormatGridColumns()
        {
            DevExpressHelper.SetColumnAsCurrency(colValor);
            var gridColumns = new List<GridColumn> { colValor };
            DevExpressHelper.ShowGroupSummaryAsCurrency(gridView, gridColumns);
        }

        #endregion

        void GridViewDoubleClick(object sender, EventArgs e)
        {
            if (!_controller.ExistsOpenedEstimate(this, "Deseja mesmo abrir o orçamento?"))
                return;
            var estimate = (OrcamentoDataSet.OrcamentoRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (estimate == null)
                return;
            _controller.ShowEstimate(estimate.Id);
        }

        #region IGridView

        public GridView GridView()
        {
            return gridView;
        }

        public string ListName
        {
            get { return "HouseRental:EstimateList"; }
        }

        #endregion
    }
}
